/**
 * 权限声明：
 */
package com.handy.webwork.action.result;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.handy.webwork.action.AbstractAction;
import com.handy.webwork.config.HandyConfiguration;

/**
 * 重定向。
 * 
 * @author rocken.zeng@gmail.com
 * 
 */
public class Redirect {

	public Redirect(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
		if (log.isDebugEnabled()){
			log.info("视图：重定向。");
		}
	}

	/**
	 * 当result类型为redirect时，重定向到指定控制器。request范围内的数据不能共享。
	 * 
	 * @param action
	 * @param resultValue
	 * @throws IOException
	 */
	public void doRedirect(AbstractAction action, String resultValue) {
		if (null == resultValue || "".equals(resultValue))
			return;
		String redirectURL = "";
		// 判断是否带有参数
		if (resultValue.indexOf('?') > -1) {
			redirectURL = resultValue.substring(0, resultValue.indexOf('?'))
					+ "."
					+ HandyConfiguration
							.properties(HandyConfiguration.PROP_SUFFIX);
			redirectURL += "?";
			String _temp = resultValue.substring(resultValue.indexOf('?') + 1);
			String[] temps = _temp.split("&");
			for (String temp : temps) {
				String[] values = temp.split("=");
				if (values.length != 2)
					continue;// 是否为键值对
				String _value = values[1];
				if (_value.startsWith("$")) {// 为变量
					_value = _value.substring(1);
					if (null != _value && !"".equals(_value)) {
						try {
							String encoderValue = BeanUtils.getProperty(action,
									_value);
							if (encoderValue == null) {
								encoderValue = "";
							} else {// 非空则编码
								encoderValue = URLEncoder
										.encode(
												encoderValue,
												HandyConfiguration
														.properties(HandyConfiguration.PROP_ENCODING));
							}

							redirectURL += values[0] + "=" + encoderValue + "&";
						} catch (IllegalAccessException e) {
							log.error(e.getMessage());
						} catch (InvocationTargetException e) {
							log.error(e.getMessage());
						} catch (NoSuchMethodException e) {
							log.error(e.getMessage());
						} catch (UnsupportedEncodingException e) {
							log.error(e.getMessage());
						}
					}
				} else {// 如果为固定变量
					if (_value == null) {
						_value = "";
					} else {
						try {
							_value = URLEncoder.encode(_value, HandyConfiguration
									.properties(HandyConfiguration.PROP_ENCODING));
						} catch (UnsupportedEncodingException e) {
							log.error(e.getMessage());
						}
					}
					redirectURL += values[0] + "=" + _value + "&";
				}

			}
		} else {
			redirectURL = resultValue
					+ "."
					+ HandyConfiguration
							.properties(HandyConfiguration.PROP_SUFFIX);
		}
		redirectURL = request.getContextPath() + "/" + redirectURL;
		if (log.isDebugEnabled()) {
			log.debug("redirectURL:" + redirectURL);
		}
		try {
			response.sendRedirect(redirectURL);
		} catch (IOException e) {
			log.error(e.getMessage());
		}
	}

	private static Log log = LogFactory.getLog(Redirect.class);

	private HttpServletRequest request;
	private HttpServletResponse response;

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
}
